<?php
/**
 * Created by PhpStorm.
 * User: ttl
 * Date: 2019/5/25
 * Time: 13:35
 */

namespace app\common\validate;

use think\Db;
use think\Validate;

class EditRegion extends Validate
{
    protected $rule = [
        'region'  =>  'require|max:50|min:1|check_name:',
    ];
    protected $message  =   [
        'region.require'=>'请输入名称',
        'region.max'=>'名称最多50字',
        'region.min'=>'请输入名称',
        'region.check_name'=>'已经有相同的区域了'
    ];

    public function check_name($value){
        $id = input('id/d',0);
        if($id>0) {
            $result = Db::name('Region')->where(['id'=>['<>',$id],'region'=>$value])->find();
            if(!empty($result)){
                return false;
            }
        }
        else{
            $result = Db::name('Region')->where(['region'=>$value])->find();
            if(!empty($result)){
                return false;
            }
        }
        return true;
    }
}